[#assign menucode="publishProduct"]
[#assign hasAttachFile = product?? && product.attachfile?? && product.attachfile != '' && product.attachfile != '#']
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="description" content="安装通（azt365.com）中国大型的b2b电子商务智慧化平台,整合了国内安装行业内采购商与供应商,打通上下游产业链,信息透明共享,通过云平台,大数据,为各大企业供应商提供产品信息、企业推广,为您提供全面的安装行业资讯金融服务以及线上支付.">
    <meta name="keywords" content="安装通,azt365,江苏b2b电子商务,江苏建筑采供批发平台,江苏弱电批发,江苏消防报警系统,江苏智慧安装建筑安装,江苏安防监控系统,江苏通风设备安装,江苏通风工程安装,江苏通风工程施工,江苏消防工程材料批发,江苏消防工程材料">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>发布商品-完善信息</title>
    <link href="/res/css/respective/company/aztCom.css" rel="stylesheet">
    <link href="/res/css/mall/mall.css" rel="stylesheet">
    <link href="/res/third/select2/css/select2.min.css" rel="stylesheet">
    <style type="text/css">
        .msg-box {
            display: inline-block;
            height: 20px;
            margin: 0;
            vertical-align: middle;
        }
        .lableIterm span.selection{
            margin-right: 0;
        }
        .lableIterm.brand span{
        	margin-right: 0;
        }
        .lastRow{
            border-bottom: 1px solid #ebebeb;
        }
        #skuTable input[type=text] {
            font-weight: normal;
        }
    </style>
</head>
<body>
<div class="warp">
[#include "/front/common/front_headAzt.html"]
    <div class="backcontent">
    [#include "/front/common/front_leftAzt.html"]
        <div class="back-main">
            <div class="mainContent">
                <form id="productForm">
                    <input type="hidden" name="productId" value="[#if product??]${product.id!}[/#if]">
                    <div class="mainContentWhite">
                        <h1>发布商品
                            [#--<a class="malView">预览</a>--]
                        </h1>
                        <hr>
                        [#-- 只有在发布页面才显示 --]
                        [#if !product??]
                        <div class="malPro">
                            <dl style="width: 23%;">
                                <dt>选择分类</dt>
                            </dl>
                            <dl style="width: 15%;">
                                <dd></dd>
                            </dl>
                            <dl style="width: 29%;">
                                <dt style="background-image: url(/res/images/respective/mall/liuCheng/liuCheng_05.png);">完善商品信息</dt>
                            </dl>
                            <dl style="width: 17%;">
                                <dd></dd>
                            </dl>
                            <dl style="width: 14%;">
                                <dt style="background-image: url(/res/images/respective/mall/liuCheng/liuCheng_07.png);color: #666666;">发布商品</dt>
                            </dl>
                        </div>
                        [/#if]

                        <div class="lableIterm">
                            <input type="hidden" name="categoryFId" value="${cates.firstId!}">
                            <input type="hidden" name="categorySId" value="${cates.secondId!}">
                            <input type="hidden" name="categoryTId" value="${cates.thirdId!}">
                            <label>所属分类：</label>
                            <span style="color: #666;vertical-align: middle;">${cates.firstName!}-${cates.secondName!}-${cates.thirdName!}</span>
                            [#-- 只有在发布的商品的时候才能修改分类 --]
                            [#if !product??]
                            <a class="modi" href="javascript:void(0)" onclick="chooseCate()">修改</a>
                            [/#if]
                        </div>
                        <div class="lableIterm">
                            <label><span>*</span>商品名称：</label>
                            <input type="text" name="productName" data-rule="required;length(~50)" data-display="商品名称" [#if product??]value="${product.name!}"[/#if]/>
                        </div>
                        <div class="lableIterm">
                            <label>卖点：</label>
                            <input type="text" name="subName" [#if product??]value="${product.subName!}"[/#if]/>
                        </div>
                        <div class="lableIterm">
                            <label>型号：</label>
                            <input type="text" name="model" [#if product??]value="${product.model!}"[/#if]/>
                        </div>
                        <div class="lableIterm brand">
                            <label>所属品牌：</label>
                            <div class="ppSelect" style="width: 267px;border: none;vertical-align: middle;">
                                <select style="color: #b9b9b9;" name="brandId">
                                    <option value=""></option>
                                    [#list brandList as brand]
                                        <option value="${brand.id!}" [#if (product.brandId)?? && product.brandId == brand.id]selected[/#if]>${brand.name!}</option>
                                    [/#list]
                                </select>
                            </div>
                            <em style="vertical-align: middle;">没有您需要的品牌？请<a href="tencent://message/?uin=2853909665&Site=www.azt365.com&Menu=yes">联系客服</a> 添加</em>
                            <span id="brandTip"></span>
                        </div>

                        <div class="lableIterm">
                            <label><span>*</span>市场价：</label>
                            <i style="position: relative;">
                                <input class="small" type="text" name="price" [#if product??]value="${product.price!}"[/#if]
                                       data-display="市场价" data-rule="required;range(0~, false)" data-target="#priceTip" data-msg-range="请输入正确的市场价">
                                <i class="danWei mall">元</i>
                            </i>
                            <span id="priceTip"></span>
                        </div>
                        <div class="lableIterm">
                            <label><span>*</span>会员价：</label>
                            <i style="position: relative;">
                                <input class="small" type="text" name="memberPrice" [#if product??]value="${product.memberPrice!}"[/#if]
                                       data-display="会员价" data-rule="required;range(0~, false);match(lte, price)" data-target="#memberPriceTip" data-msg-range="请输入正确的会员价">
                                <i class="danWei mall">元</i>
                            </i>
                            <span id="memberPriceTip"></span>
                        </div>
                        <div class="lableIterm">
                            <label><span>*</span>商品数量：</label>
                            <input class="small" type="text" name="changeStock" data-display="商品数量" data-rule="required;integer(+)" data-target="#changeStockTip" data-msg-integer="请输入正确的商品数量">
                            <span id="changeStockTip"></span>
                        </div>

                        [#if attrList?? && attrList?size > 0]
                        <div class="lableIterm">
                        	<label>商品规格：</label>
                        	<button class="specButt disab" type="button" onclick="addSku(this)">+添加规格</button>
                        	<span style="color: #999;margin-left: 5px;">请勾选以下至少一个规格，再添加</span>
                        	<div class="spec">
                                [#list attrList as attr]
                                    [#if attr.itemStr?? && attr.itemStr != '' && attr.itemStr != '[]']
                                        <dfn data-id="${attr.id!}" data-name="${attr.name!}" data-item="${attr.itemStr!}"></dfn><span>${attr.name!}</span>
                                    [/#if]
                                [/#list]
                        	</div>
                            <div style="padding-left: 120px;">
                                <div id="skuDiv" style="overflow-x: auto;"></div>
                            </div>

                        </div>
                        [/#if]

						<div class="lableIterm">
                            <label>计量单位：</label>
                            <div class="ppSelect" style="width: 60px;">
                                <select style="color: #b9b9b9;" name="unit">
                                [#list unitList as unit]
                                    <option value="${unit.code!}" [#if (product.unit)?? && unit.code == product.unit]selected[/#if]>${unit.name!}</option>
                                [/#list]
                                </select>
                            </div>
                        </div>
                        <div class="lableIterm">
                            <label>质保期：</label>
                            <input class="small" type="text" name="serviceDuration" [#if product??]value="${product.serviceDuration!}"[/#if]>
                            <div class="ppSelect" style="width: 60px;">
                                <select style="color: #b9b9b9;" name="serviceUnit">
                                    <option value="月" [#if product?? && product.serviceUnit! == '月']selected[/#if]>月</option>
                                    <option value="年" [#if product?? && product.serviceUnit! == '年']selected[/#if]>年</option>
                                </select>
                            </div>
                            <span id="serviceDurationTip"></span>
                        </div>
                        <div class="lableIterm">
                            <label>售后保障：</label>
                            <textarea type="text" placeholder="补充说明各配件的保障时间" name="service" style="border: 1px solid #e9e9e9; width: 400px;">[#if product??]${product.service!}[/#if]</textarea>
                        </div>
                        <div class="lableIterm" id="picDiv">
                            <label><span>*</span>商品图：</label>
                            <div class="picOuter">
                                [#if product?? && product.picPath??]
                                    [#list product.picPath?split(';') as pic]
                                        <div class="addZhiZhao">
                                            <img style="vertical-align: middle; height: 114px; width: 114px;" src="${pic!}">
                                            <i class="delPic" onclick="delPic(this)">×</i>
                                        </div>
                                    [/#list]
                                [/#if]
	                            <div class="addZhiZhao">
	                            	<img style="vertical-align: middle;max-width: 120px;" src="/res/images/respective/mall/upPic_03.jpg" onclick="uploadPic()">  
	                            </div>
                            </div>
                            <div class="zhiZhao"> 
                                <h6>请上传800*800像素的图片，支持JPG、PNG、JPEG、BMP格式，单张大小不超出5M，最多上传10张</h6>
                            </div>
                        </div>
                        <div class="lableIterm">
                            <label><span>*</span>详细介绍：</label>
                            <div class="jianJie" style="display: inline-block;">
                                <script id="descr" name="descr" type="text/plain">[#if product??]${product.descr!}[/#if]</script>
                            </div>
                        </div>
                        <div class="lableIterm">
                            <label>技术参数：</label>
                            <div style="display: [#if hasAttachFile]none[#else]inline-block[/#if];" id="attachUpload">
                                <button class="techData" style="background-color: #f8f8f8;color: #666;" type="button" onclick="uploadAttach()">选择文件</button>
                                <small>详细技术参数建议以EXCEL文件方式上传，平台将提供给采购商下载。</small>
                            </div>
                            <div style="display: [#if hasAttachFile]inline-block[#else]none[/#if];" id="attachDetail">
                                [#if hasAttachFile]
                                    <a href="${product.attachfile!}" id="attach">${product.attachName!}</a>
                                    [#else]
                                    <a href="#" id="attach"></a>
                                [/#if]
                                &nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="deleteAttach()">删除</a>
                                <button type="button" style="padding: 5px 15px; color: white; margin: 0 15px;" onclick="uploadAttach()">重新选择</button>
                            </div>

                        </div>
                        <div class="lableIterm" style="margin-top: 30px;">
                            <button class="opJingYing" style="margin-left: 124px;width: 150px;height: 40px;" type="button" onclick="saveProduct()">立即保存</button>
                        </div>
                    [#if !product??]
                        [@rule_content code="publish_product"/]
                    [/#if]
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 商品规格table模板(不包含tr) -->
<script type="text/html" id="skuTableTemplate">
    <table class="godSpec" id="skuTable" style="display: block;">
        <thead>
        <tr class="handHead">
            {{# $.each(d.list, function() { }}
            <th>{{ this.name }}</th>
            {{# }) }}
            <th>市场价（元）</th>
            <th>会员价（元）</th>
            <th>商品数量</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="skuTbody"></tbody>
    </table>
</script>

<!-- 商品规格tr模板 -->
<script type="text/html" id="skuTbodyTemplate">
    <tr class="lastRow" data-skuid="{{ d.skuId||'' }}">
        {{# $.each(d.list, function(i, v1) { }}
        <td>
            <select data-id="{{ v1.id }}" data-name="{{ v1.name }}">
                {{# $.each(this.item, function(i, v2) { }}
                    {{# if(d.sku && d.sku != '' && (';' + d.sku + ';').indexOf(';' + v1.id + ':' + v2.id + ';') >= 0) { }}
                    <option value="{{ v2.id }}" selected>{{ v2.name }}</option>
                    {{# } else { }}
                    <option value="{{ v2.id }}">{{ v2.name }}</option>
                    {{# } }}
                {{# }) }}
            </select>
        </td>
        {{# }) }}
        <td>
            <dfn><input type="text" data-name="price" value="{{ d.price||'' }}"></dfn>
        </td>
        <td>
            <dfn><input type="text" data-name="memberPrice" value="{{ d.memberPrice||'' }}"></dfn>
        </td>
        <td>
            <dfn><input type="text" data-name="changeStock" data-old="{{ d.changeStock||'' }}" value="{{ d.changeStock||'' }}"></dfn>
        </td>
        <td class="del" onclick="removeSku(this)" style="cursor: pointer;"></td>
    </tr>
</script>

[#include "/front/common/front_footerAzt.html"]

<script src="/res/third/layer/layer.js" type="text/javascript"></script>
<script src="/res/third/layer/layer.extends.js" type="text/javascript"></script>
<script src="/res/third/nice-validator/jquery.validator.min.js?local=zh-CN" type="text/javascript"></script>
<script type="text/javascript" src="/res/third/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/res/third/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="/res/third/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="/res/third/laytpl/laytpl.js"></script>
<script src="/res/third/select2/js/select2.full.min.js" type="text/javascript"></script>
<script src="/res/third/select2/js/i18n/zh-CN.js" type="text/javascript"></script>
<script src="/res/js/common/jQuery.numInput.js"></script>
<script type="text/javascript">
    /*window.onbeforeunload = function(){
        return "离开页面信息会丢失，是否确定要离开";
    }*/
    var ue;
    $(function () {
        [#if !companyExt??]
            layer.alert("您尚未开通店铺", {title : '提示',btn : ['去开通'], closeBtn : 0},
                    function() {
                        location.href = '/shop/setting';
                    }
            );
            return;
        [/#if]

        loadSku();
        $('select[name=brandId]').select2({
            language: "zh-CN",
            placeholder: "请选择"
        });
        var option = {
            theme : "default",
            lang : "zh-cn",
            zIndex : 1,
            initialFrameWidth : 800,
            initialFrameHeight : 200,
            autoHeightEnabled : false,
            pasteplain : false,//默认纯文本格式粘贴,
            elementPathEnabled : false,
            focus : false,//初始化时不让编辑器获取焦点
            wordCount : false,
            enableAutoSave : false, //关闭自动保存
            toolbars : [ [ 'fullscreen', 'undo', 'redo', 'bold',
                'indent', 'italic', 'underline', 'strikethrough',
                'justifyleft', 'justifyright', 'justifycenter',
                'justifyjustify', 'rowspacingtop', 'rowspacingbottom',
                'lineheight', 'removeformat', '|', 'fontfamily',
                'fontsize', 'forecolor', 'backcolor', 'insertorderedlist',
                'insertunorderedlist', 'insertimage', 'emotion',
                'inserttable', 'mergeright', 'mergedown', 'deleterow',
                'deletecol', 'splittorows', 'splittocols', 'splittocells',
                'deletecaption', 'mergecells', 'deletetable' ] ]
        };
        ue = UE.getEditor('descr', option);

        //初始化校验
        $('#productForm').validator({
            msgWrapper : 'div',
            msgMaker: function(opt){
                return '<span class="tip null" style="line-height: initial;">' + opt.msg + '</span>';
            },
            timely : 3
        });
        $('#productForm input[name=price]').numInput();
        $('#productForm input[name=memberPrice]').numInput();
        $('#productForm input[name=changeStock]').numInput({decimalLength : 0});
        $('#productForm input[name=serviceDuration]').numInput({decimalLength : 1});


        //商品规格checkbox点击事件绑定
        $(".spec dfn").click(function() {
            //若已经添加了规格tr，则不允许再变更规格checkbox
            if(!$('#skuTable') || $('#skuTable').length == 0) {
                $(this).toggleClass("chose");
                if ($(this).parent("div").find(".chose").size() == 0) {
                    $(this).parents(".lableIterm").find("button").addClass("disab");
                    $("table.godSpec").hide();
                } else {
                    $(this).parents(".lableIterm").find("button").removeClass("disab");
                }
            }
        });
    });

    function loadSku() {
        [#if product??]
        layer.ready(function() {
            $.ajax({
                url: '/product/listSku',
                type: 'post',
                dataType: 'json',
                data: {productId: '${product.id!}'},
                beforeSend: function () {
                    layer.load(2, {shade: 0.1});
                },
                success: function (res) {
                    if (res && res.success) {
                        var data = res.obj.data;
                        if (res.obj.alone) {
                            //没有规格
                            $('#productForm input[name=price]').val(data.price);
                            $('#productForm input[name=memberPrice]').val(data.memberPrice);
                            $('#productForm input[name=changeStock]').val(data.changeStock);
                            $('#productForm input[name=changeStock]').attr('data-old', data.changeStock);
                            $('#productForm input[name=skuId]').val(data.skuId);
                            //无规格商品更新的时候，此处removeSkuId先暂时存入skuId，若该无规格的商品后面变成了有规格的商品，则提交的时候会提交此参数，否则不会提交此参数
                            $('#productForm input[name=removeSkuId]').val(data.skuId);
                        } else {
                            //有规格
                            $.each(res.obj.options.split(','), function () {
                                $(".spec dfn[data-id=" + this + "]").click();
                            });
                            //组装数据
                            $.each(data, function () {
                                var array = [];
                                $.each($(".spec dfn.chose"), function () {
                                    var id = $(this).attr('data-id');
                                    var name = $(this).attr('data-name');
                                    var item = eval($(this).attr('data-item'));
                                    array.push({id: id, name: name, item: item});
                                });
                                createSkuTable({
                                    skuId: this.skuId,
                                    price: this.price,
                                    memberPrice: this.memberPrice,
                                    changeStock: this.changeStock,
                                    sku: this.sku,
                                    list: array
                                });
                            });


                        }
                    } else {
                        layer.alert('获取商品价格信息失败，请稍后重试');
                    }
                },
                complete: function () {
                    layer.closeAll('loading');
                }
            });
        });
        [/#if]
    }

    function uploadPic() {
        //最多上传10张图片
        var existPicNum = $('#picDiv .picOuter .addZhiZhao img').not(':last').length;
        if(existPicNum >= 10) {
            layer.alert('最多上传10张商品图');
            return;
        }
        layer.fileManager({
            multiple : true,
            callback : function(data) {
                //最多上传10张图片
                if(data && data.length > 0 && data.length + existPicNum > 10) {
                    layer.alert("最多上传10张商品图");
                    return;
                }
                $.each(data, function() {
                    var pic = '<div class="addZhiZhao"><img style="vertical-align: middle; height: 114px; width: 114px;" src='+ this.url + '>'
                    +'<i class="delPic" onclick="delPic(this)">×</i></div>';
                    $('#picDiv div.picOuter').prepend(pic);
                });
            }
        });
    }
    
    function delPic(o){
    	$(o).parent(".addZhiZhao").remove();
    }

    /**
     * 添加规格
     * */
	function addSku(o) {
		if($(o).hasClass("disab")) {
			return;
		} else {
            var data = [];
            $.each($(".spec dfn.chose"), function () {
                var id = $(this).attr('data-id');
                var name = $(this).attr('data-name');
                var item = eval($(this).attr('data-item'));
                data.push({id : id, name : name, item : item});
            });
            createSkuTable({list : data});
		}
	}

	/**
     * 生成规格Table
     * */
	function createSkuTable(data) {
        //若skuTable不存在，先生成table，再生成tr
        if(!$('#skuTable') || $('#skuTable').length == 0) {
            $('#skuDiv').html(laytpl($('#skuTableTemplate').html()).render(data));

            $('#productForm input[name=price]').prop('disabled', true);
            $('#productForm input[name=memberPrice]').prop('disabled', true);
            $('#productForm input[name=changeStock]').prop('disabled', true);
            $('#priceTip').html('');
            $('#memberPriceTip').html('');
            $('#changeStockTip').html('');
        }
        $('#skuTbody').append(laytpl($('#skuTbodyTemplate').html()).render(data));
        $('#skuTbody tr').not(':last').removeClass('lastRow');
        $('#skuTbody tr:last input[type=text][data-name=price]').numInput();
        $('#skuTbody tr:last input[type=text][data-name=memberPrice]').numInput();
        $('#skuTbody tr:last input[type=text][data-name=changeStock]').numInput({decimalLength : 0});
    }

	function removeSku(obj) {
        removeTr(obj);
        /*var $tr = $(obj).parents('tr');
        var skuId = $tr.attr('data-skuid')
        if(skuId != '') {
            removeTr(obj);
            layer.confirm('确定要删除吗？', function () {
                $.ajax({
                    url : '/product/removeProSku',
                    type : 'post',
                    dataType : 'json',
                    data : {skuId : skuId},
                    beforeSend : function() {
                        layer.load(2, {shade: 0.1});
                    },
                    success : function(res) {
                        if(res && res.success) {
                            removeTr(obj);
                            layer.msg('操作成功');
                        } else {
                            layer.msg('操作失败');
                        }
                    },
                    complete : function() {
                        layer.closeAll('loading');
                    }
                });
            });
        } else {
            removeTr(obj);
        }*/
    }

    function removeTr(obj) {
        $(obj).parents('tr').remove();
        $('#skuTbody tr:last').addClass('lastRow');
        if($('#skuTbody tr').length == 0) {
            //全清空了
            $('#skuTable').remove();
            $('#productForm input[name=price]').prop('disabled', false);
            $('#productForm input[name=memberPrice]').prop('disabled', false);
            $('#productForm input[name=changeStock]').prop('disabled', false);
        }
    }

    function uploadAttach() {
        layer.fileManager({
            initType : 'file',
            fileSingleSizeLimit : 10485760,
            callback : function(data) {
                $('#attach').prop('href', data[0].url);
                $('#attach').text(data[0].name);
                $('#attachDetail').css('display', 'inline-block');
                $('#attachUpload').hide();
            }
        });
    }

    function deleteAttach() {
        $('#attachDetail').hide();
        $('#attachUpload').css('display', 'inline-block');
        $('#attach').prop('href', '#');
        $('#attach').text('');
    }

    function chooseCate() {
        if(confirm("离开页面信息会丢失，是否确定要离开？")) {
            location.href = '/product/chooseCate';
        }
    }

    function saveProduct() {
        $('#productForm').isValid(function(v) {
            if(v) {
                //校验插件验证通过
                //另外还需验证商品图、商品详细介绍、详细技术参数
                if($('#picDiv .picOuter .addZhiZhao img').not(':last').length == 0) {
                    layer.msg('请上传商品图');
                    return;
                }
                if(ue.getContentLength() == 0) {
                    layer.msg('请输入商品详细介绍');
                    return;
                }

                var picPath = $('#picDiv .picOuter .addZhiZhao img').not(':last').map(function() {
                    return $(this).prop('src');
                }).get().join(';');     //商品图
                var attachfile = $('#attach').prop('href');
                var attachName = $('#attach').html();

                var isOk = true;
                //商品规格
                var skuList = [];
                if($('#skuTable') && $('#skuTable').length > 0) {
                    //添加了商品规格
                    var array = [];   //用于判断商品规格是否重复
                    $.each($('#skuTbody tr'), function() {
                        //[{skuId : xx, price : xx, memberPrice : xx, changeStock : xx, skuAttrList : [{nameId : xx, valueId : xx, name : xx, value : xx}...]}...]

                        if($(this).find('select option:selected').length != $(this).find('select').length) {
                            layer.msg('请选择属性');
                            isOk = false;
                            return false;
                        }

                        var skuId = $(this).attr('data-skuid');
                        var price = Number($(this).find('input[data-name=price]').val());
                        var memberPrice = Number($(this).find('input[data-name=memberPrice]').val());
                        var newStock = Number($(this).find('input[data-name=changeStock]').val());
                        var oldStock = Number($(this).find('input[data-name=changeStock]').attr('data-old'))||0;
                        var changeStock = newStock - oldStock;
                        var skuAttrList = [];

                        if(!price || !/^([1-9]\d*|0)(\.\d{1,2})?$/.test(price)) {
                            layer.msg('请输入正确的市场价');
                            isOk = false;
                            return false;
                        }
                        if(!memberPrice || !/^([1-9]\d*|0)(\.\d{1,2})?$/.test(memberPrice)) {
                            layer.msg('请输入正确的会员价');
                            isOk = false;
                            return false;
                        }
                        if(price < memberPrice) {
                            layer.msg("会员价不应大于市场价");
                            isOk = false;
                            return false;
                        }
                        if(!newStock || !/^[1-9]+\d*$/.test(newStock)) {
                            layer.msg('请输入正确的商品数量');
                            isOk = false;
                            return false;
                        }

                        var sku = '';
                        $.each($(this).find('select'), function() {
                            var name = $(this).attr('data-name');
                            var nameId = $(this).attr('data-id');
                            var value = $(this).find('option:selected').text();
                            var valueId = $(this).val();
                            skuAttrList.push({name : name, value : value, nameId : nameId, valueId : valueId});
                            sku += nameId + ':' + valueId + ';';
                        });
                        array.push(sku);
                        skuList.push({skuId : skuId, price : price, memberPrice : memberPrice, changeStock : newStock, skuAttrList : skuAttrList});
                    });
                    if(array.length > $.unique(array).length) {
                        layer.msg('商品规格不允许重复');
                        return;
                    }
                } else {
                    //没有添加商品规格，单商品
                    //修改的时候若之前也是无规格的商品，则将removeSkuId置为空
                    $('#productForm input[name=removeSkuId]').val('');
                    var skuId = $('#productForm input[name=skuId]').val();
                    var price = $('#productForm input[name=price]').val();
                    var memberPrice = $('#productForm input[name=memberPrice]').val();
                    var newStock = Number($('#productForm input[name=changeStock]').val());
                    var oldStock = Number($('#productForm input[name=changeStock]').attr('data-old'))||0;
                    var changeStock = newStock - oldStock;
                    skuList.push({skuId : skuId, price : price, memberPrice : memberPrice, changeStock : newStock});
                }

                if(isOk) {
                    var data = $('#productForm').serialize() + '&picPath=' + picPath + '&attachfile=' + attachfile
                            + '&attachName=' + attachName + '&skuList=' + JSON.stringify(skuList);
                    publishProduct(data);
                }
            }
        });
    }
    
    function publishProduct(data) {
        $.ajax({
            url : '/product/publish',
            type : 'post',
            dataType : 'json',
            data : data,
            beforeSend : function() {
                layer.load(2, {shade: 0.1});
            },
            success : function(res) {
                if(res && res.success) {
                    location.replace('/product/success');
                } else {
                    layer.alert(res.msg);
                }
            },
            complete : function() {
                layer.closeAll('loading');
            }
        });
    }
</script>
</body>
</html>
